package com.zhunaer.service.impl;

import com.zhunaer.dao.UserDao;
import com.zhunaer.entity.User;
import com.zhunaer.service.UserService;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Override
    public User getUserById(int userId) {
        User user = new User();
        user.setUserId(userId);
        user.setBalance(new BigDecimal(1000));
        return user;
    }

    @Override
    public boolean register(User user) {
        if(userDao.findByUsername(user.getUsername()) != null) {
            return false;
        }
        return userDao.createUser(user) > 0;
    }

    @Override
    public User login(String username, String password) {
        User user = userDao.findByUsername(username);
        if(user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public boolean updateUserInfo(User user) {
        return userDao.updateUser(user) > 0;
    }

    @Override
    public boolean recharge(int userId, BigDecimal amount) {
        return userDao.updateBalance(userId, amount) > 0;
    }

    @Override
    public void deductBalance(User user, BigDecimal amount) {
        // 实现扣减余额逻辑
        userDao.updateBalance(user.getUserId(), amount.negate());
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public int createUser(User user) {
        return userDao.createUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    @Override
    public int updateBalance(int userId, BigDecimal amount) {
        return userDao.updateBalance(userId, amount.setScale(2, RoundingMode.HALF_UP));
    }
}